<template>
  <div class="goodsinfo-container">    
    <!-- 购物车小球动画 -->
    <transition @before-enter="beforeEnter" @enter="enter"  @after-enter="afterEnter">
    <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    
    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper-box :lunbotuList="lunbotuList" :isFull="false"></swiper-box>
        </div>
      </div>
    </div>
    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsinfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价:<del>￥{{goodsinfo.market_price}}</del>&nbsp;&nbsp;
            销售价: <span class="now-price">￥{{goodsinfo.sell_price}}</span>
          </p>
          <!-- 将父组件的值通过属性绑定交给子组件 -->
          <p>购买数量 <num-box @getCont="getSelectedCount" 
          :max="goodsinfo.stock_quantity"></num-box></p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addBall">加入购物车</mt-button>
            <!-- 实现加入购物车的数值变化 ：由于涉及了父子组件的嵌套，数据是单向流的，所以需要通过事件调用机制来
                 获取子组件goodsinfo_numbox中的值。将子组件的值通过参数传递给父组件。
            -->
          </p>
        </div>
      </div>
    </div>

    <!-- 商品详情区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号:{{goodsinfo.goods_no}}</p>
          <p>库存情况:{{goodsinfo.stock_quantity}}</p>
          <p>上架时间:{{goodsinfo.add_time | dateFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>   

<script>
import { Toast } from "mint-ui";
//导入数字选择框
import Goodsinfo_numbox from "../subcomponents/goodsinfo_numbox.vue";

import Swiper from "../subcomponents/swiper.vue";
export default {
  data() {
    return {
      id:this.$route.params.id,
      lunbotuList:[],//获取轮播图数据
      goodsinfo:{}, //获取商品详情
      ballFlag:false, //小球的标示
      selectedCount:1 //选取商品的数量，默认用户选择一件
    };
  },
  created() {
    this.getLunbotu();
    this.getGoodsinfo();
  },
  methods: {
    //获取轮播图数据
    getLunbotu() {
      this.$http.get("api/getthumimages/"+this.id).then(result => {
        // console.log(result.body);
        if (result.body.status === 0) {
          //成功，接收数据
          //因为swiper组件中是使用itme.img来接收的，并没用item.src,所以需要将接收回来的数据进行处理、
          result.body.message.forEach(item =>{
            item.img = item.src;
          })
          this.lunbotuList = result.body.message;
        } else {
          // 失败，提示用户
          Toast("加载轮播图失败");
        }
      });
    },
    getGoodsinfo(){
      this.$http.get("api/goods/getinfo/"+this.id).then(result =>{
        if(result.body.status === 0){
          this.goodsinfo=result.body.message[0];
          // console.log(this.goodsinfo);
        }
      })
    },
    goDesc(id){
      this.$router.push({ name:"goodsdesc" , params:{id} });
    },
    goComment(id){
      this.$router.push({ name:"goodscomment" , params:{id} });
    },
    addBall(){
      //点击加入购物车显示小球
      this.ballFlag = true;
      // 将数字选择框内的数值恢复为默认值1
      // this.selectedCount=1;
    },
    beforeEnter(el){
      el.style.transform = "translate(0px,0px)";
    },
    enter(el,done){
      //实现小球的完美动画
      //使用Element.getBoundingClientRect()获取元素的一组矩形的集合（left、top、right、bottom）
      // 小球的DOM节点可以使用this.$refs.refname获取
      //获取到小球和购物车徽标的集合
      // 用徽标的top减去小球的top可得小球纵向移动的距离
      // 用徽标的left减去小球的left可得小球横向移动的距离
      const ballRect = this.$refs.ball.getBoundingClientRect();
      const badgeRect = document.getElementById("badge").getBoundingClientRect();
      
      const xDist = badgeRect.left - ballRect.left;
      const yDist = badgeRect.top - ballRect.top;
      
      el.offsetWidth;
      el.style.transform = `translate(${xDist}px, ${yDist}px)`;//使用ES6模板字符串进行拼接
      el.style.transition = "all 1s cubic-bezier(.4,-0.3,1,.68)"
      done();
    },
    afterEnter(el){
        this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count){
      this.selectedCount = count;
      // console.log("-------"+this.selectedCount);
    }
  },
  components: {
    "swiper-box": Swiper,
    "num-box":Goodsinfo_numbox
  }
};
</script>

<style lang="scss" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;

  .now-price{
    color: red;
    font-size: 16px;
    font-weight: bolder;
  }
  .mui-card-footer{
    display: block;
    button{
      margin:15px 0;
    }
  }
  .ball{
    width: 16px;
    height: 16px;
    background-color: red;
    border-radius: 50%;
    position:absolute;
    z-index: 99;
    top:388px;
    left:138px;
  }
}
</style>